home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Screenblankers / GBlanker / GSource / Blankers / FlyingToaster / blank.c next >
C/C++ Source or Header  |  1996-09-26  |  7KB  |  293 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include "/includes.h"
  11.  
  12. #define    IM_WIDTH  64
  13. #define    IM_HEIGHT 64
  14. #define IM_WIDBUF 84
  15. #define IM_HEIBUF 84
  16.  
  17. #define OBJECTS 0
  18. #define SPEED   2
  19. #define MODE    4
  20.  
  21. typedef struct _Toaster
  22. {
  23.     LONG delay;
  24.     LONG x, y;
  25.     LONG old_x, old_y;
  26.     LONG xspeed, yspeed;
  27.     LONG phase;
  28.     LONG xcol, ycol;
  29. } Toaster;
  30.  
  31. Toaster *Toasters;
  32. LONG NumToasters;
  33.  
  34. #include "images.h"
  35.  
  36. ULONG cmap[] = {
  37.     0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x55, 0x55, 0x55, 0x88, 0x55, 0x22,
  38.     0x66, 0x66, 0x66, 0x77, 0x77, 0x44, 0x55, 0x55, 0x66, 0xEE, 0x55, 0x22,
  39.     0x88, 0x88, 0x88, 0x99, 0x99, 0x99, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC, 0xCC,
  40.     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF };
  41.  
  42. #define abs( x ) ( (x) < 0 ? -(x) : (x) )
  43.  
  44. #include "FlyingToaster_rev.h"
  45. STATIC const UBYTE VersTag[] = VERSTAG;
  46.  
  47. VOID Defaults( PrefObject *Prefs )
  48. {
  49.     Prefs[OBJECTS].po_Level = 3;
  50.     Prefs[SPEED].po_Level = 6;
  51.     Prefs[MODE].po_ModeID = getTopScreenMode();
  52. }
  53.  
  54. LONG TestExtent( LONG Start, LONG Me, LONG x, LONG y )
  55. {
  56.     LONG i;
  57.     
  58.     for( i = Start; i < NumToasters; i++ )
  59.         if(( i != Me )&& !Toasters[i].delay &&
  60.            ( abs( x - Toasters[i].x ) < IM_WIDBUF )&&
  61.            ( abs( y - Toasters[i].y ) < IM_HEIBUF ))
  62.             return i;
  63.     
  64.     return -1L;
  65. }
  66.  
  67. BOOL FindLaunchPos( LONG i, LONG wid, LONG hei )
  68. {
  69.     LONG x, y;
  70.     
  71.     x = wid;
  72.     y = RangeRand( hei );
  73.     
  74.     FOREVER
  75.     {
  76.         if( x <= 2 * IM_WIDTH )
  77.             return FALSE;
  78.         
  79.         if( TestExtent( 0, i, x, y ) == -1 )
  80.         {
  81.             Toasters[i].x = Toasters[i].old_x = x;
  82.             Toasters[i].y = Toasters[i].old_y = y;
  83.             Toasters[i].xspeed = RangeRand( 3 ) + 1;
  84.             Toasters[i].yspeed = RangeRand( 2 ) + 1;
  85.             Toasters[i].phase = RangeRand( IMAGEMAX );
  86.  
  87.             return TRUE;
  88.         }
  89.         
  90.         if( y > -IM_HEIGHT )
  91.             y -= IM_HEIGHT;
  92.         else
  93.             x -= IM_WIDTH;
  94.     }
  95. }
  96.  
  97. LONG ActXSpeed( LONG i )
  98. {
  99.     if( Toasters[i].xcol == -1 )
  100.         return Toasters[i].xspeed;
  101.     else
  102.         return ActXSpeed( Toasters[i].xcol );
  103. }
  104.  
  105. LONG ActYSpeed( LONG i )
  106. {
  107.     if( Toasters[i].ycol == -1 )
  108.         return Toasters[i].yspeed;
  109.     else
  110.         return ActXSpeed( Toasters[i].ycol );
  111. }
  112.  
  113. LONG Blank( PrefObject *Prefs )
  114. {
  115.     LONG delay_rate, i, j, counter = 0, RetVal = OK;
  116.     struct RastPort *Rast;
  117.     struct Screen *FTScr;
  118.     struct Window *FTWin;
  119.     struct Window *Wnd;
  120.     
  121.     NumToasters = Prefs[OBJECTS].po_Level;
  122.     delay_rate = 11 - Prefs[SPEED].po_Level;
  123.     
  124.     Toasters = AllocVec( sizeof( Toaster ) * NumToasters, MEMF_CLEAR );
  125.     FTScr = OpenScreenTags( NULL, SA_DisplayID, Prefs[MODE].po_ModeID,
  126.                            SA_Depth, 4, SA_Overscan, OSCAN_STANDARD,
  127.                            SA_Type, CUSTOMSCREEN, SA_Quiet, TRUE,
  128.                            SA_Behind, TRUE, TAG_DONE );
  129.     if( Toasters && FTScr )
  130.     {
  131.         for( i = 0; i < 48; i += 3 )
  132.             if( GfxBase->lib_Version < 39 )
  133.                 SetRGB4(&( FTScr->ViewPort ), i/3, ( cmap[i] * 16 ) / 256,
  134.                         ( cmap[i+1] * 16 ) / 256, ( cmap[i+2] * 16 ) / 256 );
  135.             else
  136.                 SetRGB32(&( FTScr->ViewPort ), i/3, cmap[i]<<24, cmap[i+1]<<24,
  137.                          cmap[i+2]<<24 );
  138.         FTWin = OpenWindowTags( 0L, WA_Width, FTScr->Width, WA_Height,
  139.                                FTScr->Height, WA_IDCMP, 0L, WA_Flags,
  140.                                WFLG_SIMPLE_REFRESH|WFLG_BORDERLESS,
  141.                                WA_CustomScreen, FTScr, TAG_DONE );
  142.         if( FTWin )
  143.         {
  144.             Rast = FTWin->RPort;
  145.             SetAPen( Rast, 0 );
  146.             
  147.             for( i = 0; i < NumToasters; i++ )
  148.                 if( !FindLaunchPos( i, ( LONG )FTWin->Width,
  149.                                    ( LONG )FTWin->Height ))
  150.                     Toasters[i].delay = 30;
  151.             
  152.             Wnd = BlankMousePointer( FTScr );
  153.             ScreenToFront( FTScr );
  154.             
  155.             while( RetVal == OK )
  156.             {
  157.                 WaitTOF();
  158.                 
  159.                 if(!( ++counter % 60 ))
  160.                     ScreenToFront( FTScr );
  161.                 
  162.                 if(!( counter % delay_rate ))
  163.                 {
  164.                     for( i = 0; i < NumToasters; i++ )
  165.                     {
  166.                         if( !Toasters[i].delay )
  167.                         {
  168.                             Toasters[i].old_x = Toasters[i].x;
  169.                             Toasters[i].old_y = Toasters[i].y;
  170.                             Toasters[i].x -= Toasters[i].xspeed;
  171.                             Toasters[i].y += Toasters[i].yspeed;
  172.                             Toasters[i].xcol = -1;
  173.                             Toasters[i].ycol = -1;
  174.                         }
  175.                     }
  176.  
  177.                     for( i = 0; i < NumToasters; i++ )
  178.                     {
  179.                         if( !Toasters[i].delay )
  180.                         {
  181.                             j = -1;
  182.                             while(( j = TestExtent( j+1, i, Toasters[i].x,
  183.                                                    Toasters[i].y )) >= 0 )
  184.                             {
  185.                                 if( abs( Toasters[j].old_x -
  186.                                         Toasters[i].old_x ) < IM_WIDBUF )
  187.                                 {
  188.                                     if( Toasters[i].y < Toasters[j].y )
  189.                                         Toasters[i].ycol = j;
  190.                                     if( Toasters[i].xspeed ==
  191.                                        Toasters[j].xspeed )
  192.                                         Toasters[i].xspeed++;
  193.                                 }
  194.                                 else
  195.                                 {
  196.                                     if( Toasters[i].x > Toasters[j].x )
  197.                                         Toasters[i].xcol = j;
  198.                                     if( Toasters[i].yspeed ==
  199.                                        Toasters[j].yspeed )
  200.                                         Toasters[i].yspeed++;
  201.                                 }
  202.                                 if( abs( Toasters[j].old_y -
  203.                                         Toasters[i].old_y ) < IM_HEIBUF )
  204.                                 {
  205.                                     if( Toasters[i].x > Toasters[j].x )
  206.                                         Toasters[i].xcol = j;
  207.                                     if( Toasters[i].yspeed ==
  208.                                        Toasters[j].yspeed )
  209.                                         Toasters[i].yspeed++;
  210.                                 }
  211.                             }
  212.                         }
  213.                     }
  214.  
  215.                     for( i = 0; i < NumToasters; i++ )
  216.                     {
  217.                         if( !Toasters[i].delay )
  218.                         {
  219.                             Toasters[i].x = Toasters[i].old_x - ActXSpeed( i );
  220.                             Toasters[i].y = Toasters[i].old_y + ActYSpeed( i );
  221.                         }
  222.                     }
  223.                     
  224.                     for( i = 0; i < NumToasters; i++ )
  225.                     {
  226.                         if( !Toasters[i].delay )
  227.                         {
  228.                             j = -1;
  229.                             while(( j = TestExtent( j+1, i, Toasters[i].x,
  230.                                                    Toasters[i].y )) >= 0 )
  231.                             {
  232.                                 if( abs( Toasters[j].old_x -
  233.                                         Toasters[i].old_x ) < IM_WIDBUF )
  234.                                     if( Toasters[i].x > Toasters[j].x )
  235.                                         Toasters[i].x = Toasters[i].old_x;
  236.                                 else
  237.                                     if( Toasters[i].y < Toasters[j].y )
  238.                                         Toasters[i].y = Toasters[i].old_y;
  239.                                 if( abs( Toasters[j].old_y -
  240.                                         Toasters[i].old_y ) < IM_HEIBUF )
  241.                                     if( Toasters[i].y < Toasters[j].y )
  242.                                         Toasters[i].y = Toasters[i].old_y;
  243.                             }
  244.                         }
  245.                     }
  246.  
  247.                     for( i = 0; i < NumToasters; i++ )
  248.                     {
  249.                         if( !Toasters[i].delay )
  250.                         {
  251.                             Toasters[i].phase =
  252.                                 ( Toasters[i].phase + 1 ) % IMAGEMAX;
  253.                             EraseRect( Rast, Toasters[i].x + IM_WIDTH,
  254.                                       Toasters[i].old_y, Toasters[i].x +
  255.                                       IM_WIDTH + Toasters[i].xspeed,
  256.                                       Toasters[i].old_y + IM_HEIGHT );
  257.                             EraseRect( Rast, Toasters[i].old_x,
  258.                                       Toasters[i].old_y,
  259.                                       Toasters[i].old_x + IM_WIDTH,
  260.                                       Toasters[i].old_y + Toasters[i].yspeed );
  261.                             DrawImage( Rast, img[Toasters[i].phase],
  262.                                       Toasters[i].x, Toasters[i].y );
  263.                             if(( Toasters[i].x < -IM_WIDTH-1 )||
  264.                                ( Toasters[i].y > FTWin->Height ))
  265.                                 Toasters[i].delay = RangeRand( 50 );
  266.                         }
  267.                         else
  268.                             if(!( --Toasters[i].delay ))
  269.                                 Toasters[i].delay =
  270.                                     FindLaunchPos( i, ( LONG )FTWin->Width,
  271.                                                   ( LONG )FTWin->Height )
  272.                                         ? 0 : 30;
  273.                     }
  274.                 }
  275.                 RetVal = ContinueBlanking();
  276.             }
  277.  
  278.             SetSignal( 0L, SIGBREAKF_CTRL_C );
  279.             UnblankMousePointer( Wnd );
  280.             CloseWindow( FTWin );
  281.         }
  282.         else
  283.             RetVal = FAILED;
  284.         CloseScreen( FTScr );
  285.     }
  286.     else
  287.         RetVal = FAILED;
  288.     
  289.     FreeVec( Toasters );
  290.  
  291.     return RetVal;
  292. }
  293.